package com.zorn.core.filter;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.zorn.core.util.RequestUtils;
import com.zorn.platform.global.Setting;

public class AccessControlFilter implements Filter {

	private Logger log = Logger.getLogger(AccessControlFilter.class);
	
	private String publics = "";

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		log.info("Access Control Filter Init...");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		HttpSession session = request.getSession();
		
		log.debug(request.getServletPath());
		
		log.debug(request.getRequestURI());
		log.debug(request.getRequestURL());

		Map<String, Object> user = (Map<String, Object>) session.getAttribute("user");

		boolean async = RequestUtils.isAsync(request);

		if (user == null) {
			response.setStatus(403);
			if (async) {

			} else {
				if(request.getServletPath().equals("/index.html")) {
					chain.doFilter(req, res);
				} else {
					response.sendRedirect("index.html");
				}
			}
		} else {

		}

		/*// 如果有权限
		if (true) {

		} else {
			// 异步请求
			if (RequestUtils.isAsync(request)) {
				Map<String, Object> result = new java.util.concurrent.ConcurrentHashMap<String, Object>();
				result.put("success", false);
				result.put("message", Setting.getString("Zorn.403message", "权限不足，已限制访问。"));
				response.setStatus(403);
			} else {

			}
		}

		if (RequestUtils.isAsync(request)) {

		} else {
			if (true) {

			} else {

			}
			if (true) {

			} else {
				response.setStatus(403);
				response.sendRedirect("/403.html");
			}
		}*/
	}

	@Override
	public void destroy() {

	}

}
